package com.recource.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.recource.annonation.Login;
import com.recource.dao.*;
import com.recource.domain.*;
import com.recource.ro.OrderRO;
import com.recource.ro.ProductRO;
import com.recource.ro.VipRO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author xjhStart
 * @create 2023-04-30 下午1:47
 */
@Controller
@RequestMapping("/vip")
@Transactional(rollbackFor = Exception.class)
@Login
public class VipController {


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CoachMapper coachMapper;

    @Autowired
    private ProjectMapper projectMapper;


    @Autowired
    private VipMapper vipMapper;


    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     *  根据时间戳生成订单号
     * */
    public static String getOrderNo () {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        LocalDateTime localDateTime = Instant.ofEpochMilli(System.currentTimeMillis()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        return df.format(localDateTime);
    }




    //选择教练 选择充值项目付款
    @RequestMapping("topup")
    public ModelAndView topup(OrderRO ro){

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/vip/project/list");

        String vipId = ro.getVipId();
        String projectId = ro.getProjectId();
        String coachId = ro.getCoachId();

        Vip vip = vipMapper.selectById(vipId);
        Project project = projectMapper.selectById(projectId);
        Coach coach = coachMapper.selectById(coachId);

        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getCoach_id,coachId);
        wrapper.eq(Order::getVip_id,vip);
        wrapper.eq(Order::getProject_id,projectId);

        Order order = new Order();

        order.setCoach_id(String.valueOf(coach.getId()));
        order.setCoach_name(coach.getName());
        order.setVip_id(String.valueOf(vip.getId()));
        order.setVip_name(vip.getName());
        order.setProject_id(String.valueOf(project.getId()));
        order.setProject_name(project.getName());
        order.setPrice(project.getPrice());
        order.setCreate_time(new Date());

        Order selectOne = orderMapper.selectOne(wrapper);
        if(ObjectUtils.isNotEmpty(selectOne)){

            Date time = selectOne.getExpirationTime();
            Date date = addDay(time, 7);
            selectOne.setExpirationTime(date);

            orderMapper.updateById(selectOne);
        }else {
            order.setOrder_number(getOrderNo());
            orderMapper.insert(order);
        }
        return modelAndView;
    }


    /**
     * 在原日期的基础上增加天数
     * @param date
     * @param i
     * @return
     */
    public static Date addDay(Date date,int i){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, i);
        Date newDate = c.getTime();
        return newDate;
    }


    //评价教练
    @RequestMapping("evaluate")
    public ModelAndView Evaluate(Evaluate ro){
        ModelAndView modelAndView = new ModelAndView();
        evaluateMapper.insert(ro);
        modelAndView.setViewName("/vip/evaluate");

        return modelAndView;
    }


    //购买商品
    @RequestMapping("buy")
    public ModelAndView buy(ProductRO ro){

        ModelAndView modelAndView = new ModelAndView();

        Vip vip = vipMapper.selectById(ro.getVipId());
        Product product = productMapper.selectById(ro.getProductId());

        Order order = new Order();
        order.setOrder_number(getOrderNo());
        order.setVip_id(String.valueOf(vip.getId()));
        order.setVip_name(vip.getName());
        order.setProduct_id(String.valueOf(product.getId()));
        order.setProduct_name(product.getName());
        order.setCreate_time(new Date());
        orderMapper.insert(order);
        modelAndView.setViewName("/vip/buy/success");

        return modelAndView;
    }

    //TODO 预约教练
    @RequestMapping("make")
    public ModelAndView make(VipRO ro){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/make/success");
        coachMapper.make(ro.getVipId(),ro.getCoachId());
        return modelAndView;
    }

}
